package utils;

import java.io.File;

import javax.xml.bind.JAXB;

import utils.verifier.ArgumentVerifier;
import container.WeaponTraitDescriptions;
import container.WeaponTraitDescription;
import container.Weapons;

/**
 * This enumerator contains method to store and load the specific xml files.
 * 
 * @author Marcus
 * 
 */
public enum XMLFileHandler {
	; /* No Instance needed. */

/**
	 * Stores the passed {@link Weapons) object into an xml-file which has the
	 * name of the passed string.
	 * 
	 * @param weapons	the object which is stored
	 * @param filename	the name of the file
	 * 
	 * @throws IllegalArgumentException if the passed parameters are {@code null} or the filename is empty.
	 */
	public static void storeWeapons(Weapons weapons, String filename) {
		ArgumentVerifier.ensureNotNull(weapons,
				"The passed weapons may not be 'null'");
		ArgumentVerifier.ensureNotNull(filename,
				"The passed filename may not be 'null'");
		ArgumentVerifier.ensureNotEmpty(filename,
				"The passed filename may not be empty.");

		JAXB.marshal(weapons, new File(filename));
	}

/**
	 * Stores the passed {@link WeaponTraitDescriptions) object into an xml-file which has the
	 * name of the passed string.
	 * 
	 * @param weapontraits	the object which is stored
	 * @param filename	the name of the file
	 * 
	 * @throws IllegalArgumentException if the passed parameters are {@code null} or the filename is empty.
	 */
	public static void storeWeaponTraits(WeaponTraitDescriptions weapontraits,
			String filename) {
		ArgumentVerifier.ensureNotNull(weapontraits,
				"The passed weapons may not be 'null'");
		ArgumentVerifier.ensureNotNull(filename,
				"The passed filename may not be 'null'");
		ArgumentVerifier.ensureNotEmpty(filename,
				"The passed filename may not be empty.");

		JAXB.marshal(weapontraits, new File(filename));
	}

	/**
	 * Loads an {@link Weapons} object form the xml-file which is specified by
	 * the passed filename.
	 * 
	 * @param filename
	 *            the file which is loaded
	 * 
	 * @return the loaded object
	 */
	public static Weapons loadWeapons(String filename) {
		ArgumentVerifier.ensureNotNull(filename,
				"The passed filename may not be 'null'");
		ArgumentVerifier.ensureNotEmpty(filename,
				"The passed filename may not be empty.");

		return JAXB.unmarshal(new File(filename), Weapons.class);
	}

	/**
	 * Loads an {@link WeaponTraitDescriptions} object form the xml-file which is
	 * specified by the passed filename.
	 * 
	 * @param filename
	 *            the file which is loaded
	 * 
	 * @return the loaded object
	 */
	public static WeaponTraitDescriptions loadWeaponTraits(String filename) {
		ArgumentVerifier.ensureNotNull(filename,
				"The passed filename may not be 'null'");
		ArgumentVerifier.ensureNotEmpty(filename,
				"The passed filename may not be empty.");

		return JAXB.unmarshal(new File(filename), WeaponTraitDescriptions.class);
	}
}
